/**
 * Copyright (C), 2019-2022, 宇信融汇
 * FileName: DelayedConfig
 * Author:   666
 * Date:     2022/4/22 18:34
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 666           修改时间           版本号              描述
 */
package com.mashibing.rabbitmq_boot.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname DelayedConfig
 * @Description 延迟交换机
 * @Date 2022/4/22 18:34
 * @Created by 666
 */
@Configuration
public class DelayedConfig {
    public static final String DELAYED_EXCHANGE = "delayed-exchange";
    public static final String DELAYED_QUEUE = "delayed-queue";
    public static final String DELAYED_ROUTING_KEY = "delayed.#";
    @Bean
    public Exchange delayedExchange(){
        Map<String, Object> arguments = new HashMap<>();
        //需要设置x-delayed-type 延迟队列的类型
        arguments.put("x-delayed-type", "topic");
        //创建需要使用customEchange 设置x-delayed-message
        Exchange exchange = new CustomExchange(DELAYED_EXCHANGE, "x-delayed-message", true, false, arguments);
        return exchange;
    }
    @Bean
    public Queue delayedQueue(){
        return QueueBuilder.durable(DELAYED_QUEUE).build();
    }

    @Bean
    public Binding delayedBinding(Queue delayedQueue,Exchange delayedExchange){
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();
    }
}
